public class Solution783 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int last=Integer.MIN_VALUE/2;
    int min=Integer.MAX_VALUE;

    public int minDiffInBST(TreeNode root) {
        dfs(root);
        return min;
    }

    private void dfs(TreeNode root) {
        if (root.left!=null){
            dfs(root.left);
        }
        min=Math.min(min,root.val-last);
        last=root.val;
        if (root.right!=null){
            dfs(root.right);
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode=new TreeNode(4,new TreeNode(2,new TreeNode(1),new TreeNode(3)),new TreeNode(6));
        System.out.println(new Solution783().minDiffInBST(treeNode));
    }
}
